#include <assert.h>
#include <imbus.h>
#include <stdio.h>
#include <string.h>

void print_attribute (IMPointer attr, IMPointer user_data)
{
    static char *underline_names[] = { "None", "Single", "Double", "Wave" };
    static char *font_style_names [] = { "Normal", "Oblique", "Italic" };
    static char *font_weight_names [] = { "Normal", "Bold" };

    IMType type = IM_ATTRIBUTE_TYPE (attr);

    printf ("%p: %16s %3d -%3d: ",
            attr,
            im_type_get_name (IM_ATTRIBUTE_TYPE (attr)),
            im_attribute_get_start (attr),
            im_attribute_get_end (attr));

    if (IM_TYPE_ATTR_LANGUAGE == type)
        printf ("%s", im_attr_language_get (attr));
    else if (IM_TYPE_ATTR_FONT_FAMILY == type)
        printf ("%s", im_attr_font_family_get (attr));
    else if (IM_TYPE_ATTR_STRING == type)
        printf ("%s", im_attr_string_get (attr));
    else if (IM_TYPE_ATTR_FONT_STYLE == type)
        printf ("%s", font_style_names [im_attr_font_style_get (attr)]);
    else if (IM_TYPE_ATTR_FONT_WEIGHT == type)
        printf ("%s", font_weight_names [im_attr_font_weight_get (attr)]);
    else if (IM_TYPE_ATTR_FONT_SIZE == type)
        printf ("%d", im_attr_font_size_get (attr));
    else if (IM_TYPE_ATTR_FONT_SCALE == type)
        printf ("%d", im_attr_font_scale_get (attr));
    else if (IM_TYPE_ATTR_UNDERLINE == type)
        printf ("%s", underline_names [im_attr_underline_get (attr)]);
    else if (IM_TYPE_ATTR_FOREGROUND == type) {
        IMUInt16 red, green, blue;
        im_attr_foreground_get (attr, &red, &green, &blue);
        printf ("R=%u G=%u B=%u", red, green, blue);
    } else if (IM_TYPE_ATTR_BACKGROUND == type) {
        IMUInt16 red, green, blue;
        im_attr_background_get (attr, &red, &green, &blue);
        printf ("R=%u G=%u B=%u", red, green, blue);
    } else if (IM_TYPE_ATTR_UNDERLINE_COLOR == type) {
        IMUInt16 red, green, blue;
        im_attr_underline_color_get (attr, &red, &green, &blue);
        printf ("R=%u G=%u B=%u", red, green, blue);
    } else if (IM_TYPE_ATTR_STRIKETHROUGH_COLOR == type) {
        IMUInt16 red, green, blue;
        im_attr_strikethrough_color_get (attr, &red, &green, &blue);
        printf ("R=%u G=%u B=%u", red, green, blue); 
    } else if (IM_TYPE_ATTR_OBJECT == type)
        printf ("%s\n", im_type_get_name (im_object_get_type (im_attr_object_get (attr))));

    printf ("\n");
}

int main (int argc, char *argv [])
{
    IMAttribute *attr;
    IMAttrList  *list;
    IMAttrList  *sublist;
    IMAttrIterator *iter;

    im_init (&argc, &argv);
#if IM_ENABLE_DEBUG
    _im_slice_print_debug_info();
#endif

    list = im_attr_list_new ();

    attr = im_attr_language_new ("zh-cn");
    im_attribute_set_range (attr, 1, 10);
    im_attr_list_insert (list, attr);

    attr = im_attr_font_family_new ("Sans");
    im_attribute_set_range (attr, 2, 8);
    im_attr_list_insert (list, attr);

    attr = im_attr_font_style_new (IM_ATTR_FONT_STYLE_ITALIC);
    im_attribute_set_range (attr, 3, 12);
    im_attr_list_insert (list, attr);

    attr = im_attr_font_size_new (20000);
    im_attribute_set_range (attr, 5, 7);
    im_attr_list_insert (list, attr);

    attr = im_attr_font_scale_new (120);
    im_attribute_set_range (attr, 4, 8);
    im_attr_list_insert (list, attr);

    attr = im_attr_underline_new (IM_ATTR_UNDERLINE_SINGLE);
    im_attribute_set_range (attr, 1, 5);
    im_attr_list_insert (list, attr);

    attr = im_attr_underline_new (IM_ATTR_UNDERLINE_SINGLE);
    im_attribute_set_range (attr, 7, 16);
    im_attr_list_insert (list, attr);

    printf ("Original list:\n");
    im_attr_list_foreach (list, print_attribute, 0);

    printf ("Change with Attribute:\n");
    attr = im_attr_underline_new (IM_ATTR_UNDERLINE_SINGLE);
    im_attribute_set_range (attr, 4, 8);
    print_attribute (attr, 0);
    im_attr_list_change (list, attr);

    printf ("Changed list:\n");
    im_attr_list_foreach (list, print_attribute, 0);

    printf ("Change with Attribute:\n");
    attr = im_attr_underline_new (IM_ATTR_UNDERLINE_DOUBLE);
    im_attribute_set_range (attr, 4, 8);
    print_attribute (attr, 0);
    im_attr_list_change (list, attr);

    printf ("Changed list:\n");
    im_attr_list_foreach (list, print_attribute, 0);

    printf ("Change with Attribute:\n");
    attr = im_attr_font_style_new (IM_ATTR_FONT_STYLE_OBLIQUE);
    im_attribute_set_range (attr, 0, 6);
    print_attribute (attr, 0);
    im_attr_list_change (list, attr);

    printf ("Changed list:\n");
    im_attr_list_foreach (list, print_attribute, 0);

    printf ("Change with Attribute:\n");
    attr = im_attr_font_size_new (12000);
    im_attribute_set_range (attr, 8, 12);
    print_attribute (attr, 0);
    im_attr_list_change (list, attr);

    printf ("Changed list:\n");
    im_attr_list_foreach (list, print_attribute, 0);

    printf ("Change with Attribute:\n");
    attr = im_attr_font_size_new (20000);
    im_attribute_set_range (attr, 1, 4);
    print_attribute (attr, 0);
    im_attr_list_change (list, attr);

    printf ("Changed list:\n");
    im_attr_list_foreach (list, print_attribute, 0);

    sublist = im_attr_list_get_sub_list (list, 3, 5);

    printf ("Sublist pos=3 len=5:\n");
    im_attr_list_foreach (sublist, print_attribute, 0);

    im_attr_list_stretch (sublist, 1, 5);

    printf ("Sublist stretched pos=1 len=5:\n");
    im_attr_list_foreach (sublist, print_attribute, 0);

    im_attr_list_shrink (list, 4, 4);
    printf ("list shrunk pos=4 len=4:\n");
    im_attr_list_foreach (list, print_attribute, 0);

    printf ("Iterator list:\n");
    iter = im_attr_list_get_iterator (list);

    do {
        print_attribute (im_attr_iterator_get (iter), 0);
    } while (im_attr_iterator_next (iter)); 

    im_attr_list_optimize (list);
    printf ("Optimized list:\n");
    im_attr_list_foreach (list, print_attribute, 0);

    im_attr_list_clear_range (list, 3, 2);
    printf ("list clear range pos=3 len=2:\n");
    im_attr_list_foreach (list, print_attribute, 0);

    im_attr_list_remove (list, 1, 2, IM_TYPE_ATTR_FONT_SIZE);
    printf ("list remove AttrFontSize pos=1 len=2:\n");
    im_attr_list_foreach (list, print_attribute, 0);

    im_attr_list_remove (list, 7, 2, IM_TYPE_ATTR_UNDERLINE);
    printf ("list remove AttrUnderline pos=7 len=2:\n");
    im_attr_list_foreach (list, print_attribute, 0);

    im_attr_iterator_destroy (iter);
    im_object_unref (sublist);

    im_object_unref (list);

#if IM_ENABLE_DEBUG
    _im_slice_print_debug_info();
#endif
    return 0;
}
/*
vi:ts=4:nowrap:ai:expandtab
*/
